package com.platform.common.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpUtils {
    /**
     * 向指定 URL 发送POST方法的请求
     *
     * @param url    发送请求的 URL
     * @param params 请求的参数集合
     * @return 远程资源的响应结果
     */
    @SuppressWarnings("unused")
    public static String sendPost(String url, String params) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new StringEntity(params));
        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        String responseContent = EntityUtils.toString(entity, "UTF-8");
        response.close();
        httpClient.close();
        return responseContent;
    }

    /**
     * post 发送form表单类型的请求(Content-Type:application/x-www-form-urlencoded)
     *
     * @param url    路径
     * @param params 参数列表
     * @return
     * @throws Exception
     */
    public static String sendPostForm(String url, Map<String, String> params, String reqCharset, String resCharset) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);

        // 根据params 解析参数列表
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        for (String key : params.keySet()) {
            formparams.add(new BasicNameValuePair(key, params.get(key)));
        }
        UrlEncodedFormEntity reqEntity = new UrlEncodedFormEntity(formparams, reqCharset);
        httpPost.setEntity(reqEntity);
        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        String responseContent = EntityUtils.toString(entity, resCharset);
        response.close();
        httpClient.close();
        return responseContent;
    }


    /**
     * 向指定 URL 发送GET方法的请求
     *
     * @param url    发送请求的 URL
     * @return 远程资源的响应结果
     */
    @SuppressWarnings("unused")
    public static String sendGet(String url) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        String responseContent = EntityUtils.toString(entity, "UTF-8");
        response.close();
        httpClient.close();
        return responseContent;
    }
    /**
     * 向指定 URL 发送GET方法的请求
     *
     * @param url    发送请求的 URL
     * @param params 请求的参数集合
     * @return 远程资源的响应结果
     */
    @SuppressWarnings("unused")
    public static String sendGet(String url, String params) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        String responseContent = EntityUtils.toString(entity, "UTF-8");
        response.close();
        httpClient.close();
        return responseContent;
    }

    @SuppressWarnings("unused")
    public static String sendGet(String url, String params, HttpHost proxy) throws Exception {
        CloseableHttpClient httpClient = HttpClientBuilder.create().setProxy(proxy).build();
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        String responseContent = EntityUtils.toString(entity, "UTF-8");
        response.close();
        httpClient.close();
        return responseContent;
    }

    public static void main(String[] args) throws Exception {
//        JSONObject js = JSONObject.parseObject(sendGet("https://apis.map.qq.com/ws/geocoder/v1/?address=%E8%80%92%E9%98%B3%E5%" +
//                "B8%82%E8%94%A1%E5%AD%90%E6%B1%A0%E8%A1%97%E9%81%93%E9%87%8E%E9%B9%85%E5%A1%98%E5%8D%97%E5%B7%B7&key=XIBBZ-I3IWD-Z7O4S-H5TYK-5X3PF-DDFL4"));
        JSONObject js = JSONObject.parseObject(
                "{\"result\":{\"level\":31,\"similarity\":0.8,\"ad_info\":{\"adcode\":\"430481\"},\"reliability\":7,\"location\":{\"lng\":112.849798,\"lat\":26.405809},\"deviation\":1000,\"title\":\"野鹅塘南巷\",\"address_components\":{\"province\":\"湖南省\",\"city\":\"衡阳市\",\"street\":\"\",\"district\":\"耒阳市\",\"street_number\":\"\"}},\"message\":\"query ok\",\"status\":0}"
        );
        System.out.println(js.toString());
        System.out.println(js.getJSONObject("result"));
        System.out.println(js.getJSONObject("result").getJSONObject("location"));
        System.out.println(js.getIntValue("status") == 0);
        if (js.getIntValue("status") == 0) {
            System.out.println(js.getJSONObject("result").getJSONObject("location").getString("lat"));
            System.out.println(js.getJSONObject("result").getJSONObject("location").getString("lng"));
        }
    }

}
